package com.winsomely.leecode;

/**
 * 密钥格式化
 * 
 * @author liujiawei
 *
 */
public class LicenseKeyFormatting {

	public String licenseKeyFormatting(String S, int K) {
		StringBuilder builder = new StringBuilder();
		for (int i = S.length() - 1, j = 0; i >= 0; i--) {
			char c = S.charAt(i);
			if (c == '-') {
				continue;
			}
			if (j == K) {
				builder.insert(0, '-');
				j = 0;
			}			
			builder.insert(0, Character.toUpperCase(c));
			j++;
		}
		return builder.toString();
	}

	public static void main(String[] args) {
		LicenseKeyFormatting t = new LicenseKeyFormatting();
		String s = "--a-a-a-a--";
		int k = 2;
		System.out.println(t.licenseKeyFormatting(s, k));
	}

}
